//
//  SLProductViewModel.m
//  StarLight
//
//  Created by Duke.wu on 16/5/18.
//  Copyright © 2016年 Duke.Wu. All rights reserved.
//

#import "SLNearlyProductDetailViewModel.h"
#import "SLNetWork.h"

@interface SLNearlyProductDetailViewModel ()

@property (nonatomic, strong) NSString *lxfs;

@end

@implementation SLNearlyProductDetailViewModel

#pragma mark - Detail

- (instancetype)initWithGid:(NSString *)gid{
  self = [super init];
  if (self) {
    [self getDataGid:gid];
  }
  return self;
  
}

- (void)getDataGid:(NSString *)gid{
  NSString *requst = [NSString stringWithFormat:@"%@/goods/goodsallinfo?gid=%@",baseUrl,gid];
  
  @weakify(self);
  [SLNetWork getDataWithRequest:requst complete:^(id respond, NSError *error) {
    @strongify(self);
    if (!error) {
      
      SLGoodsDetailModel *model = [SLGoodsDetailModel mj_objectWithKeyValues:respond[@"data"]];
      self.detailModel = model;
    }
  }];
  
  [self connectGoodsOwnerWithGid:gid];
}

- (void)connectGoodsOwnerWithGid:(NSString *)gid{
  NSString *requst = [NSString backUrlWithService:@"goods" methodName:@"contactowner"];
  @weakify(self);
  NSDictionary *postDic = @{@"gid":gid,@"uid":[SLNetWork backUid]};
  [SLNetWork postDataWithParameters:postDic postUrl:requst complete:^(id respond, NSError *error) {
    @strongify(self)
    if (!error) {
      self.lxfs = respond[@"lxfs"];
    }
  }];
}

- (void)connectGoodsOwner{
   [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",self.lxfs]]];
}



#pragma mark - More

- (instancetype)initWithHid:(NSString *)hid{
  self = [super init];
  if (self) {
    [self getMoreGoodsWith:hid];
  }
  return self;
}

- (void)getMoreGoodsWith:(NSString *)hid{
  @weakify(self)
  NSString *requst = [NSString stringWithFormat:@"%@/goods/usergoods?hid=%@&uid=%@",baseUrl,hid,[SLNetWork backUid]];
  [SLNetWork getDataWithRequest:requst complete:^(id respond, NSError *error) {
    @strongify(self)
    if (!error) {
      self.moreModel = [SLMoreGoodsModel mj_objectWithKeyValues:respond[@"data"]];
    }
  }];

}

- (void)refreshWith:(NSString *)hid{
   [self getMoreGoodsWith:hid];
}






@end


@implementation SLGoodsDetailModel

+ (NSDictionary *)mj_replacedKeyFromPropertyName{
  return @{@"hid":@"id"};
}


@end

@implementation SLMoreGoodsModel

+ (NSDictionary *)mj_objectClassInArray{
  return @{@"goods":[SLNearLyProduct class]};
}

+ (NSDictionary *)mj_replacedKeyFromPropertyName{
  return @{@"hid":@"id"};
}

@end




